#ifndef __SCWIN32_H
#define __SCWIN32_H

#include <psos.h>
#include <sys_conf.h>
#include <stdio.h>
#include <time.h>
#include <limits.h>
#include <Rhapsody.h>
#include <RapError.h>

#ifdef __cplusplus
extern "C" {
#endif

#define MAIN           sctmmain
#define LOG_FUNC       NULL
#define UINT_FAST8_MAX 255
#define PATH_MAX       512
#define AF_UNIX        1
#define AF_INET        2
#define SOCK_STREAM    1
#define SOCK_DGRAM     2
#define IPPROTO_TCP    6
#define IPPROTO_UDP    17
#define	INADDR_ANY     (unsigned int)0x00000000
#define ECONNREFUSED   61
#define SCOS_MTU       512
#define MSG_WAITALL    0
#define M_PI           3.14159265358979323846264338327
#define SOL_SOCKET     0
#define SO_REUSEADDR   0

#define SCOS_PRIO         1
#define SCOS_PRIO_WAIT    1
#define SCOS_PRIO_PROCESS 2
#define SCOS_PRIO_CONTROL 3

#ifdef LIBDYNAMIC
#error "TriMedia Target doesn't support dynamically loaded libraries"
#endif

#undef LIBIMPORTSYMBOL
#define LIBIMPORTSYMBOL
#undef LIBEXPORTSYMBOL
#define LIBEXPORTSYMBOL
#undef LIBCTOR
#define LIBCTOR
#undef LIBDTOR
#define LIBDTOR
#undef LIBSYMBOL
#define LIBSYMBOL

struct in_addr
{
	unsigned long    s_addr;
};

struct sockaddr
{
	int sa_family;
	char sa_data[14];
};

struct sockaddr_in
{
    short            sin_family;
    unsigned short   sin_port;
    struct in_addr   sin_addr;
    char             sin_zero[8];
};

struct hostent
{
   char   *h_name;
   char  **h_aliases;
   short   h_addrtype;
   short   h_length;
   char  **h_addr_list;
};

struct timeval
{
   long int tv_sec;
   long int tv_usec;
};

typedef unsigned long   scos_mutex_t;
typedef unsigned long   scos_thread_t;
typedef unsigned long   scos_cond_t;
typedef time_t          suseconds_t;
typedef char            int_fast8_t;
typedef unsigned char   uint_fast8_t;
typedef unsigned char   uint8_t;
typedef int             SOCKET;

typedef struct scos_socket_s
{
	int bound, listening, connected;
	struct sockaddr local_addr, remote_addr;
	int local_addrlen, remote_addrlen;
	unsigned long qid;

	scos_cond_t condition;
} scos_socket_t;

//#define malloc(size) scos_malloc(size)
//#define calloc(nmemb, size) scos_calloc(nmemb, size)
#define strcasecmp      strcmp       /* Case sensitive */
#define strncasecmp     strncmp      /* Case sensitive */
#ifndef htons
#define htons(x)        (x)
#endif

LIBSYMBOL int  scos_thread_create(scos_thread_t *thread, void *(*func)(void*), void *arg);
LIBSYMBOL int  scos_thread_join(scos_thread_t *thread);
LIBSYMBOL int  scos_mutex_init(scos_mutex_t *mutex);
LIBSYMBOL int  scos_mutex_destroy(scos_mutex_t *mutex);
LIBSYMBOL int  scos_mutex_lock(scos_mutex_t *mutex);
LIBSYMBOL int  scos_mutex_unlock(scos_mutex_t *mutex);
LIBSYMBOL int  scos_mutex_trylock(scos_mutex_t *mutex);
LIBSYMBOL int  scos_cond_init(scos_cond_t *cond);
LIBSYMBOL int  scos_cond_wait(scos_cond_t *cond, scos_mutex_t *mutex);
LIBSYMBOL int  scos_cond_signal(scos_cond_t *cond);
LIBSYMBOL int  scos_cond_destroy(scos_cond_t *cond);
LIBSYMBOL void scos_setpri(unsigned char prio);
LIBSYMBOL void scos_setpreempt(unsigned char yesno);
LIBSYMBOL int  scos_self(void);
LIBSYMBOL void scos_init(void);
LIBSYMBOL void scos_fini(void);
LIBSYMBOL void scos_dump(void);
LIBSYMBOL void*scos_malloc(size_t size);
LIBSYMBOL void*scos_calloc(size_t nmemb, size_t size);

LIBSYMBOL void usleep(unsigned long int usec);
LIBSYMBOL int  gettimeofday(struct timeval *tv, void *dummy);
LIBSYMBOL void sched_yield(void);
LIBSYMBOL struct hostent *gethostbyname(char *name);

int             scos_socket    (int af, int type, int protocol);
int             scos_accept    (int s, struct sockaddr *addr, int  *addrlen);
int             scos_bind      (int s, struct sockaddr *addr, int   addrlen );
int             scos_connect   (int s, struct sockaddr *addr, int   addrlen );
int             scos_recv      (int s, char  * buf, int len, int flags);
int             scos_send      (int s, char  * buf, int len, int flags);
int             scos_close     (int s);
int             scos_listen    (int s, int backlog);
int             scos_sendto    (int s, char *msg, int len, int flags,
                                struct sockaddr *to, int tolen);
int             scos_setsockopt(int s, int level, int optname, const void*optval,
                                int optlen);

void scos_dump(void);
void scos_memtest(void);
void scos_trace(unsigned char op);

#ifdef __cplusplus
}
#endif

#endif /* __SCWIN32_H */
